﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ASPNETPerformance.App_BusinessLogic.BasePages;
using ASPNETPerformance.App_BusinessLogic.Helpers;
using ASPNETPerformance.App_BusinessLogic.Utils;

namespace ASPNETPerformance._09_Test {
    public partial class Tracing : BasePage {
        protected void Page_Load( object sender, EventArgs e )
        {
            this.Page.Trace.IsEnabled = CommonHelper.GetCookieBool("IsTracing");

            if ( !this.Page.IsPostBack ) {
                Trace.Warn( "Page_Load Starting" );
                //Trace.Write( "Page_Load Starting" );
            }
        }

        protected override void Render( HtmlTextWriter writer ) {
            if ( Trace.IsEnabled ) {
                System.IO.StringWriter sw = new StringWriter();
                HtmlTextWriter localWriter = new HtmlTextWriter( sw );
                base.Render( localWriter );
                string output = sw.ToString();
                int pageSize = output.Length;
                Trace.Warn( "Page Size: " + pageSize.ToString() );
                writer.Write( output );
            }
            else {
                base.Render( writer );
            }
        }

        protected override void SavePageStateToPersistenceMedium( object viewState ) {
            base.SavePageStateToPersistenceMedium( viewState );
            if ( Trace.IsEnabled ) {
                LosFormatter format = new LosFormatter();
                StringWriter writer = new StringWriter();
                format.Serialize( writer, viewState );
                int viewStateLength = writer.ToString().Length;
                Trace.Warn( "ViewState Size: " + viewStateLength.ToString() );
            }
        }
    }
}